package com.common.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.vo.Result;

/**
 * 异常处理类
 * @author YYQ
 *
 */
@ControllerAdvice
public class MyExceptionHandler {
	
	private static final Logger logger = Logger.getLogger(MyExceptionHandler.class);
	

	/**
	 * 全局异常返回json格式
	 * @param requset
	 * @param response
	 * @param ex
	 * @return
	 */
	@ExceptionHandler
	@ResponseBody
	public Result<?> handleAndReturnData(HttpServletRequest requset, HttpServletResponse response, Exception ex){
        logger.info("错误类型："+ex.getClass().getSimpleName());
        logger.info("错误信息："+ex.getMessage());
        logger.info("错误原因："+ex.getCause());
        logger.error(ex.getMessage());
        ex.printStackTrace();
		return Result.fail("操作异常："+ex.toString());
	}
}
